package org.itboys.product.web;

import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.product.entity.Product;
import org.itboys.product.entity.ProductBrand;
import org.itboys.product.entity.ProductCategorySpec;
import org.itboys.product.entity.ProductText;
import org.itboys.product.entity.ShopProductCategory;
import org.itboys.product.service.ProductBrandService;
import org.itboys.product.service.ProductCategoryService;
import org.itboys.product.service.ProductCategorySpecService;
import org.itboys.product.service.ProductService;
import org.itboys.product.service.ProductTextService;
import org.itboys.product.service.ShopProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

/**
 * 商品
 * @author weisky
 *
 */
@RestController
@RequestMapping("/product/product")
public class ProductController extends BaseController{

	@Autowired
	private ProductService productService;
	@Autowired
	private ProductCategoryService productCategoryService;
	@Autowired
	private ProductBrandService productBrandService;
	@Autowired
	private ProductTextService productTextService;
	@Autowired
	private ShopProductCategoryService shopProductCategoryService;
	@Autowired
	private ProductCategorySpecService productCategorySpecService;
	/**
	 * 跳到商品管理页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toListVM")
	public ModelAndView toListVM(HttpServletRequest request, HttpServletResponse response,Model model) {
		return new ModelAndView("/ace/product/productList");
	}

	/**
	 * 获取商品列表 带分页参数
	 * @param request
	 * @param response
     */
	@RequestMapping("/list")
	public void list(HttpServletRequest request,HttpServletResponse response){
		Page<Product> page = productService.pageQuery(request);
		for (Product product : page.getData()) {
			BigDecimal price = new BigDecimal(product.getPrice());
			BigDecimal proPrice = new BigDecimal(product.getProPrice());
			proPrice = proPrice.divide(new BigDecimal(100));
			price = price.divide(new BigDecimal(100));
			product.setPrice(price.intValue());
			product.setProPrice(proPrice.intValue());
		}
		JsonPageUtils.renderJsonPage(page.getTotal(), page.getData(), response);
	}

	/**
	 * 跳转产品编辑界面
	 * @param id
	 * @param model
     * @return
     */
	@RequestMapping("/input")
	public ModelAndView input(@RequestParam(value = "id", required = false)Long id,
							  Model model){
		if(id != null && id > 0){
			ProductText text = productTextService.getByField("productId", id);
			model.addAttribute("productText", text);
			Product product = productService.getById(id);
			model.addAttribute("product", product);
		}
		return  new ModelAndView("ace/product/productInput");
	}
	
	@RequestMapping("/save")
	public void save(Product product,
					 @RequestParam(value="specDetails", required = false)List<Long> specDetails,
					 @RequestParam(value="productSpecDetails", required = false)String productSpecDetails,
			@RequestParam(required=false) String[] tagsArr,
			@RequestParam(value="imgFile", required = false) MultipartFile imgFile,
			@RequestParam(value="imgFiles", required = false) MultipartFile[] imgFiles,
			HttpServletRequest request,HttpServletResponse response){
		boolean result = productService.insert(product, imgFile, imgFiles, specDetails, productSpecDetails);
		//插入详情
		if(request.getParameter("textContent")!=null){
			ProductText productText = productTextService.getByField("productId", product.getId());
			if(productText == null){
				productText = new ProductText();
				productText.setProductId(product.getId());
				productText.setProductText(request.getParameter("textContent"));
				productTextService.insert(productText);
			}else{
				productText.setProductText(request.getParameter("textContent"));
				productTextService.update(productText);
			}
		}
		//插入售后服务
		/*if(request.getParameter("productAfterServer")!=null){
			ProductAfterServer productAfterServer=new ProductAfterServer();
			productAfterServer.setProductId(product.getId());
			productAfterServer.setProductAfterServer(request.getParameter("productAfterServer"));
			productAfterServerService.insert(productAfterServer);
		}*/
		if(result){
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		}else{
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}

	/**
	 * 查看商品基本信息
	 * @param productId
	 * @param request
	 * @param response
     */
	@RequestMapping("/getById")
	public void getById(Long productId,
			HttpServletRequest request,HttpServletResponse response){
		AjaxUtils.renderJson(response, productService.getById(productId));
	}

	/**
	 * 查看商品详情
	 * @param productId
	 * @param request
	 * @param response
     */
	@RequestMapping("/getProductText")
	public void getProductText(Long productId,
						HttpServletRequest request,HttpServletResponse response){
		AjaxUtils.renderJson(response, productTextService.getByField("productId",productId));
	}

	/**
	 * 查看商品售后
	 * @param productId
	 * @param request
	 * @param response
     */
	@RequestMapping("/getProductAfter")
	public void getProductAfter(Long productId,
							   HttpServletRequest request,HttpServletResponse response){
		AjaxUtils.renderJson(response, productTextService.getByField("productId",productId));
	}

	/**
	 * 删除商品
	 * @param productId
	 * @param request
	 * @param response
     */
	@RequestMapping("/delete")
	public void delete(Long productId,
			HttpServletRequest request,HttpServletResponse response){
		try {
			productService.delete(productId);
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		} catch (Exception e) {
			logger.error("/product/product/delete error", e);
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}

	/**
	 * 产品规格页面
	 * @param productId  产品id
	 * @param catId  分类id
	 * @param model
     * @return
     */
	@RequestMapping("/productSpec")
	public ModelAndView productSpec(@RequestParam("productId")Long productId,
									@RequestParam("catId")Long catId,
									Model model){
		productService.getSpec(catId, productId, model);
		return new ModelAndView("ace/product/productSpecDetail");
	}

}
